/*
 * @lc app=leetcode id=128 lang=cpp
 *
 * [128] Longest Consecutive Sequence
 */
class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        int len = nums.size();
        if (len < 2) {
            return len;
        }

        map<int, int> memo;
        for (auto n : nums) {
            memo[n] = n;
        }

        int k, v;
        int max = 1, currSum;
        for (auto it : memo) {
            k = it.first;
            v = it.second;
            if (memo.find(k-1) == memo.end()) {
                currSum = 1;
                while (memo.find(v+1) != memo.end()) {
                    v++;
                    currSum++;
                }
                max = std::max(max, currSum);
            }
        }

        return max;
    }
};

